# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: inkyu

import concurrent.futures
import datetime
import logging
import sys

from coin.strategy.tool.monitor.log_parser_runner import (check_utc, run_config_v1)
from coin.strategy.tool.monitor.config import STRATEGIES


def main(argv):
  assert check_utc(), 'Invalid timezone'
  current_time = datetime.datetime.now()

  assert len(argv) > 2, 'You must provide dump_duration (hours)'
  dump_duration = datetime.timedelta(hours=int(argv[2]))
  current_time = datetime.datetime.strptime(argv[1], '%Y%m%d-%H%M%S')

  configs = STRATEGIES
  futures = []
  with concurrent.futures.ProcessPoolExecutor() as executor:
    for config in configs:
      if not isinstance(config, dict):
        continue
      fut = executor.submit(run_config_v1, config, current_time, dump_duration)
      futures.append(fut)

  balance_dump = []
  for f in futures:
    quote, result = f.result()
    balance_dump += result['dump']
  balance_dump = sorted(balance_dump, key=lambda x: (x['time'], x['name']))

  print('time_hr,name,value')
  for balance in balance_dump:
    print('"%s","%s",%f' % (balance['time'], balance['name'], balance['value']))


if __name__ == '__main__':
  logging.basicConfig(level='DEBUG', format='%(levelname)8s %(asctime)s %(name)s] %(message)s')
  try:
    sys.exit(main(sys.argv))
  except KeyboardInterrupt:
    sys.exit(1)
